package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

//文件上传映射中心
@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;

    /* @PostMapping("/upload")
     public Result Controllerupload(String username, Integer age, MultipartFile image) throws IOException {
         log.info("文件上传:{},{},{}", username, age, image);
         String OriginalFilename = image.getOriginalFilename();//获取原始文件名
         int index = OriginalFilename.lastIndexOf(".");//找到文件名最后的.所在的位置
         String extname = OriginalFilename.substring(index);//开始切割
         String newFileName = UUID.randomUUID().toString() + extname;//拼接
         image.transferTo(new File("D:\\images\\" + newFileName));//存放至本地文件夹
         return Result.success();

     }*/
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传，文件名：{}", image.getOriginalFilename());
        //调用阿里云工具类
       String url = aliOSSUtils.upload(image);

        log.info("文件上传完成，文件访问的url为", url);
        return Result.success(url);
    }
}
